<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/20 0020
 * Time: 10:32
 */

namespace app\admin\controller;


use app\model\model\Adminlog;
use app\model\model\Adminuser;
use think\Controller;
use think\Request;

class Login extends Controller
{
    /**
     * 渲染登录页和处理登录逻辑
     * */
    public function index(){
        $method = $this -> request -> method();
        if($method === 'GET'){
            return $this->fetch('index');
        }else{
            $post = input();
            $adminuser = new Adminuser();

            $data = $adminuser->getUserOneByUsername($post['username']);
            if($post['username']==""&&$post['password']==""){
                return 300;
            }else{
                if($post['username']==$data['username']){
                    if(md5($post['password'])==$data['password']){
                        $re = $adminuser->updateOneById($data['id'],['login_time'=>time()]);
                        if($re){
                            session("admin_name",$post['username']);
                            session('admin_id',$data['id']);
                            admin_log("管理员登录");
                            if($re){
                                return json_echo_msg(200,'登陆成功！');
                            }
                        }
                    }else{
                        return json_echo_msg(400,'密码不正确！');
                    }
                }else{
                    return json_echo_msg(500,'用户名不正确！');
                }
            }
        }
    }
    /**
     * 退出登录
     * */
    public function logout(){
        session("admin_name",null);
        session("admin_id",null);
        $this->redirect(WEB_URL.'admin/login/index');
    }
}